﻿using MyWPF.Models.Recipe;
using MyWPF.StaticData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MyWPF.Views.Subpage.RecipePage_Son
{
    /// <summary>
    /// MotifyRecipe.xaml 的交互逻辑
    /// </summary>
    public partial class MotifyRecipe : Window
    {
        public MotifyRecipe()
        {
            InitializeComponent();
            this.DataContext = StaticModels.vM_MotifyRecipe;
            StaticModels.vM_MotifyRecipe.ButtonContent = "添加";

            StaticModels.vM_MotifyRecipe.NewRecipe = new RecipeContext();
            StaticModels.vM_MotifyRecipe.Function = ViewModel.Subpage.RecipePage_Son.Func.Add;
            StaticModels.vM_MotifyRecipe.NameReadOnly = false;

            LoadUserControl();
        }
        public MotifyRecipe(RecipeContext Recipe)
        {
            InitializeComponent();
            this.DataContext = StaticModels.vM_MotifyRecipe;
            StaticModels.vM_MotifyRecipe.ButtonContent = "修改";

            StaticModels.vM_MotifyRecipe.NewRecipe = Recipe;
            StaticModels.vM_MotifyRecipe.Function = ViewModel.Subpage.RecipePage_Son.Func.Motify;
            StaticModels.vM_MotifyRecipe.NameReadOnly = true;

            LoadUserControl();
        }
        /// <summary>
        /// 加载右侧配方数据区域
        /// </summary>
        private void LoadUserControl()
        {
            //先初始化RecipeContext
            StaticModels.vM_MotifyRecipe.RecipeContext = new UserControl();
            StackPanel MainStack = new StackPanel();
            //遍历结构体
            Type RecipeType = typeof(Data_1);
            PropertyInfo[] properties = RecipeType.GetProperties();
            foreach (var property in properties)
            {
                string BindingObiect =  property.Name;
                Binding binding = new Binding(BindingObiect);
                binding.Source = StaticModels.vM_MotifyRecipe.NewRecipe.Data_1;

                StackPanel stackPanel = new StackPanel()
                {
                    Orientation = Orientation.Horizontal,
                };
                TextBlock textBlock = new TextBlock()
                {
                    Text = property.Name,
                    VerticalAlignment = VerticalAlignment.Center,
                    Margin = new Thickness(3),
                };
                Border border = new Border()
                {
                    Background = new SolidColorBrush(Color.FromArgb(0xff, 0xcc, 0xcc, 0xcc)),
                    CornerRadius = new CornerRadius(5,5,0,0),
                    Margin = new Thickness(3),
                };
                TextBox textBox = new TextBox()
                {
                    MinWidth = 50,
                    MaxWidth = 200,
                    VerticalContentAlignment = VerticalAlignment.Center,
                    HorizontalContentAlignment = HorizontalAlignment.Center,
                    BorderBrush = new SolidColorBrush(Color.FromArgb(0xff, 0x5f, 0x63, 0x68)),
                    CaretBrush = new SolidColorBrush(Color.FromArgb(0xff, 0x5f, 0x63, 0x68)),
                    TextWrapping = TextWrapping.Wrap,
                };
                //加绑定
                textBox.SetBinding(TextBox.TextProperty, binding);
                //控件合并
                border.Child = textBox;
                stackPanel.Children.Add(textBlock);
                stackPanel.Children.Add(border);
                MainStack.Children.Add(stackPanel);
            }
            StaticModels.vM_MotifyRecipe.RecipeContext.Content = MainStack;
        }
    }
}
